package com.fruit.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;


public class HttpClientManagerT {
	public static String getData(String url) {		 
		 HttpGet httpget = new HttpGet(url);
		 
		 return getData(httpget);
	} 
	
	public static String getData(HttpGet httpget) {
		 CloseableHttpClient httpClient=HttpClientUtil.getHttpClient();
		 
		 String res=null;        
	     CloseableHttpResponse response=null;
		 try {
	            response = httpClient.execute(httpget);
	            InputStream in=response.getEntity().getContent();
	            res=IOUtils.toString(in,"UTF-8");
	            in.close();
	        } catch (UnsupportedOperationException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }finally {            
	            if(response!=null){
	            try {
	                response.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	            }            
	        }   
		 return res;
	} 
	
	public static String postData(String url, String xmlParam){
		 
		 HttpPost httpPost = new HttpPost(url);
		 
		 return postData(httpPost, xmlParam);
	}
	
	
	public static String postData(HttpPost httpPost, String xmlParam){
		CloseableHttpClient httpClient=HttpClientUtil.getHttpClient();
		 
		 String res=null;        
	     CloseableHttpResponse response=null;
		 try {
			 httpPost.setEntity(new StringEntity(xmlParam, "UTF-8"));
	            response = httpClient.execute(httpPost);
	            
	            InputStream in=response.getEntity().getContent();
	            res=IOUtils.toString(in,"UTF-8");
	            in.close();
	        } catch (UnsupportedOperationException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }finally {            
	            if(response!=null){
	            try {
	                response.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	            }            
	        }  
		 return res;
	}	
	

	public static String postData(HttpPost httpPost){
		CloseableHttpClient httpClient=HttpClientUtil.getHttpClient();
		 
		 String res=null;        
	     CloseableHttpResponse response=null;
		 try {
	            response = httpClient.execute(httpPost);
	            
	            InputStream in=response.getEntity().getContent();
	            res=IOUtils.toString(in,"UTF-8");
	            in.close();
	        } catch (UnsupportedOperationException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }finally {            
	            if(response!=null){
	            try {
	                response.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	            }            
	        }  
		 return res;
	}		
	
	public static String postSslData(String url,Map<String, String> params, String xmlParam, LayeredConnectionSocketFactory factory){
		 
		 HttpPost httpPost = new HttpPost(url);
		 if(params != null){
			 for(String k:params.keySet()){
				 httpPost.addHeader(k,params.get(k));
			 }
		 }
		 
		 return postSslData(httpPost, xmlParam, factory);
	}
	
	
	public static String postSslData(HttpPost httpPost, String xmlParam, LayeredConnectionSocketFactory factory){
		CloseableHttpClient httpClient=HttpClientUtil.getHttpClient(factory);
		 
		 String res=null;        
	     CloseableHttpResponse response=null;
		 try {
			 httpPost.setEntity(new StringEntity(xmlParam, "UTF-8"));
	            response = httpClient.execute(httpPost);
	            
	            InputStream in=response.getEntity().getContent();
	            res=IOUtils.toString(in,"UTF-8");
	            in.close();
	        } catch (UnsupportedOperationException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }finally {            
	            if(response!=null){
	            try {
	                response.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	            }            
	        }  
		 return res;
	}		
	
	public static HttpPost getPostMethod(String url) {
		HttpPost pmethod = new HttpPost(url); // 设置响应头信�?
		pmethod.addHeader("Connection", "keep-alive");
		pmethod.addHeader("Accept", "*/*");
		pmethod.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		pmethod.addHeader("X-Requested-With", "XMLHttpRequest");
		pmethod.addHeader("Cache-Control", "max-age=0");
		pmethod.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
		
		
		return pmethod;
	}	
}
